<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>灾害体管理</title>
    <style>
        .nicescroll-rails.nicescroll-rails-vr{
            background: transparent!important;
            left:2.715rem!important;
        }
    </style>
</head>
<body>
        <div class="right-top-tools" style="height:50px">
            <div class="zd-tab clearfix" style="height:50px">
               <span style="color:#3c619e;font-weight: bold">灾害体管理  </span>
            </div>
        </div>
		<div class="table_filter"  style="height:50px;text-align:left;padding-top:6px; margin-top:30px">
			<div class="dataTables_filter" id="searchDiv">
				<input style="width: 200px;" placeholder="请输入灾害体名称" id="likeFilter" name="likeFilter" class="form-control" type="search" likeoption="true">
				<div class="btn-group" style="margin-left:6px">
					<button type="button" class="btn btn-primary" data-btn-type="search" onclick="search()">查询</button>
					<button type="button" class="btn btn-default" data-btn-type="reset" onclick="likeFilterReset()">重置</button>
				</div>
				<div class="btn-group" style="float:right;display:none" id="btns">
					<button id="zht_new" type="button" class="btn btn-default" data-btn-type="add">新增</button>
					<button id="zht_edit" type="button" class="btn btn-default" data-btn-type="edit">编辑</button>
					<button id="zht_delete" type="button" class="btn btn-default" data-btn-type="delete">删除</button>
				</div>
			</div>
		</div>
        <div class="table-container" style="margin-top:0px" >
            <table id="zht_table" class="display" cellspacing="0" width="100%">
                <thead>
                <tr>
                    <th style="text-align:center">灾害体名称</th>
                    <th style="text-align:center">所属项目名称</th>
                    <th style="text-align:center">所属地区</th>
                    <th style="text-align:center">地区经度</th>
                    <th style="text-align:center">地区纬度</th>
                    <th style="text-align:center">最后修改时间</th>
                </tr>
                </thead>
            </table>
        </div>

<script type="text/javascript">
	var Dtable_zht = null;
	function search(){
		Dtable_zht.ajax.reload()
	}
	function likeFilterReset(){
		$("#likeFilter").val("")
		Dtable_zht.ajax.reload()
	}
	
	//组织一个datatables的setting对象
	function getDatatableSettings(){
		var _pagesize = 10;
    	var _url = "../../tbl_zht/list";
		var setting =  {
		        "info":0,
		        "pageLength": _pagesize,
		        "lengthChange":false,
		        "searching":false,
		        "ordering":true,
		        "processing": true,
		        "serverSide": true,
		        "ajax": { 
		        	url: _url,  
		        	type: "post",
		        	contentType: "application/json",
		            dataType: "json",
		            data: function( data ) {
		            	if( data.order && data.order.length > 0 && data.columns && data.columns.length > 0 ){
		            		data.orderIndex = data.order[0].column;
		            		if( data.columns.length >= ( data.orderIndex + 1 )){
		            			data.orderColName = data.columns[data.orderIndex].data;
		            		}
		            		data.orderType = data.order[0].dir;
		            	}
		            	//处理组织起来的只显示用的列进行排序的情况
		            	if( data.orderColName == "areaname" ){
		            		data.orderColName = "area_code";
		            	}
		            	//模糊查询
		            	if( $("#likeFilter").val() != null && $("#likeFilter").val() != "" ){
		            		data.likeFilter = $("#likeFilter").val();
		            	}
		            	data.tree_id = global.device_data_tree_selected_id;
		            	data.tree_level = global.device_data_tree_selected_level;
		                data.page = data.start/data.length + 1;
		                data.pagesize = _pagesize;
		                return JSON.stringify(data);
		            },
		            dataSrc: function ( json ) {
		            	$(json.data).each(function(){
		            		this.update_time = new Date(this.update_time).format("yyyy年MM月dd日");
		            	});
		            	json.recordsTotal = json.pagination.totalRecord;
		            	json.recordsFiltered = json.pagination.totalRecord;
		            	return json.data;
		            }
		        },
		        "columns": [
		            { "data": "name" },
		            { "data": "projectname" },
		            { "data": "areaname" },
		            { "data": "location_lng" },
		            { "data": "location_lat" },
		            { "data": "update_time" }
		        ],
		        "language": {
		            "paginate": {
		                "next": "下一页",
		                "previous": "上一页",
		            }
		        }
		    }
		return setting;
	}
	
	function showDataTable(){
		var setting = getDatatableSettings();
		Dtable_zht = $('#zht_table').DataTable( setting );
	}
	
    $(document).ready(function(){
    	
    	if("ADMIN" == global.currentRole) {
    		$("#btns").show();
    	}
    	
    	showDataTable();
	        	
	    $('#zht_table tbody').on( 'click', 'tr', function () {
            if ( $(this).hasClass('selected') ) {
                $(this).removeClass('selected');
            }else {
                $('tr.selected').removeClass('selected');
                $(this).addClass('selected');
            }
        } );
	    
	    $("#zht_new").click( function(){
	    	global.showModal("添加新项目", "zht/zht_create.html");
	    });
	    
	    $("#zht_edit").click(function(){
	    	if( Dtable_zht.rows('.selected').data().length == 0 ){
	    		global.alert('请选择一条您需要编辑的信息！');
	    	}else{
	    		global.showModal("编辑项目信息", "zht/zht_edit.html");
	    	}	    	
	    });
	    
	    $("#zht_delete").click(function(){
	    	if( Dtable_zht.rows('.selected').data().length == 0 ){
	    		global.alert('请选择一条您需要编辑的信息！');
	    	}else{
	    		$.confirm({
	        	    title: "删除操作确认！",
	        	    content: "您确认需要删除这一条信息吗，请谨慎操作，数据可能无法恢复！",
	        	    buttons: {
	        	    	confirm:{
	        	    		btnClass: 'btn-blue',
	        	    		text: '直接删除', // Some Non-Alphanumeric characters
		                    action: function(){
		                    	var id = Dtable_zht.rows('.selected').data()[0].id;
		    		    		var url = "../../tbl_zht/" + id;
		    		    		$.ajax({
		    		    			  url: url,
		    		    			  type : 'delete',
		    		    			  async : false,
		    		    			  contentType: "application/json; charset=utf-8",
		    		    			  dataType: "json",
		    		    			  success: function(json) {
		    		    				 global.alert( json.message );
		    		    				 $("#zht_main").load("zht/zht_list.html");
		    		    			  }
		    		    		});
		                    }
	        	    	},
	        	    	cancel: {
	        	    		text: '暂不删除',
	        	        },
	        	    }
	        	});
	    	}
	    });
    });    

</script>
</body>
</html>